package com.ruoyi.web.controller.system.question;

import java.util.List;

import com.ruoyi.common.viewmodel.QuestionVo;
import com.ruoyi.common.viewmodel.question.QuestionEditRequestVM;
import com.ruoyi.common.viewmodel.question.QuestionPageRequestVM;
import com.ruoyi.common.viewmodel.question.QuestionResponseVM;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.question.service.IQuestionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题目信息Controller
 * 
 * @author ruoyi
 * @date 2023-06-23
 * 因为缺少前端表单的提交，很多功能不是很完善
 */
@Controller
@RequestMapping("/question/questionList")
public class QuestionController extends BaseController
{
    private String prefix = "question/questionList";

    @Autowired
    private IQuestionService questionService;

    @RequiresPermissions("question:questionList:view")
    @GetMapping()
    public String questionList()
    {
        return prefix + "/questionList";
    }

    /**
     * 查询题目信息列表
     */
    @RequiresPermissions("question:questionList:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QuestionPageRequestVM requestVM) {
        startPage();
        List<QuestionResponseVM> list = questionService.selectQuestionList(requestVM);
        return getDataTable(list);
    }

    /**
     * 导出题目信息列表
     */
    // TODO 返回题目的信息为JSON数据，不知道该怎么解析比较好
    @RequiresPermissions("question:questionList:export")
    @Log(title = "题目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuestionVo questionVo) {
//        List<QuestionVo> list = questionService.selectQuestionList(questionVo);
//        ExcelUtil<QuestionVo> util = new ExcelUtil<QuestionVo>(QuestionVo.class);
//        return util.exportExcel(list, "题目信息数据");
        return null;
    }

    /**
     * 新增题目信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存题目信息
     * TODO 前台表单，具体题目信息需要提交JSON
     */
    @RequiresPermissions("question:questionList:add")
    @Log(title = "题目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuestionEditRequestVM requestVM) {

        return toAjax(questionService.insertQuestion(requestVM));
    }

    /**
     * 回显题目信息
     * TODO 回显具体题目信息为JSON，需要在前端表单展示
     */
    @RequiresPermissions("question:questionList:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        QuestionVo question = questionService.selectQuestionById(id);
        mmap.put("question", question);
        return prefix + "/edit";
    }

    /**
     * 修改保存题目信息
     * TODO 前台表单，具体题目信息需要提交JSON
     */
    @RequiresPermissions("question:questionList:edit")
    @Log(title = "题目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuestionEditRequestVM requestVM)
    {
        return toAjax(questionService.updateQuestion(requestVM));
    }

    /**
     * 删除题目信息
     */
    @RequiresPermissions("question:questionList:remove")
    @Log(title = "题目信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }
}
